package com.lingyistudio.user.service.impl;

import com.lingyistudio.common.response.WebResponse;
import com.lingyistudio.common.util.Md5AndSha1;
import com.lingyistudio.common.util.UUIDModel;
import com.lingyistudio.user.dao.UserDao;
import com.lingyistudio.user.entity.CustomerEntity;
import com.lingyistudio.user.entity.UserEntity;
import com.lingyistudio.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: userServiceImpl
 * @Description:
 * @Author: lingyi  l4sFjRYLo1Up1caoVNeChuC0=
 * @Date: 2019/5/25 Zo58LWGf2HiIC2XDlEYFLSJg4p8IouwjUq9gKWhaAMuWFoc1xWHB7CRAd/VSedMzckRNYk7dqP8iayxqZ+Lyk80ZZpn4Tgn/zxE5T844xzEWWwd5fNrkd2OW46uYWham8wSruLE/oS6/Xb+j3OZ6jb89NVNxZIG1Gh0HvzZCKG4=
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    //用户sql接口
    @Resource
    UserDao userDao;

    //redis接口
    @Autowired
    RedisTemplate redisTemplate;

    //登录
    @Override
    public WebResponse userLoginService(UserEntity userEntity) {

        try {
            //判断用户名和密码是否符合标准
            if (userEntity.getUserNumber() == null || userEntity.getUserNumber().length() < 3 || userEntity.getUserPassword() == null || userEntity.getUserPassword().length() < 6) {
                return WebResponse.errorOther("用户名或格式错误");
            }
            //尝试手机登陆
            CustomerEntity customer = userDao.getUserOneNeedUserMobilephoneDao(userEntity);
            //尝试邮箱登陆
            if (customer == null) {
                log.info("手机登录失败，使用邮箱尝试登录");
                customer = userDao.getUserOneNeedUserEmallDao(userEntity);
            }
            redisTemplate.opsForValue().set("54656546", "454654545", 30, TimeUnit.MINUTES);
            redisTemplate.opsForValue().set("5465654645", "45465", 30, TimeUnit.MINUTES);
            log.info( String.valueOf(redisTemplate.opsForValue().get("54656546") ) );
            log.info( String.valueOf(redisTemplate.opsForValue().get("5465654645") ) );
            log.info("成功");
            if (customer != null) {
                //检验密码
                if (Md5AndSha1.verifyMD5AndSHA1(userEntity.getUserPassword(), customer.getCustomerPassword())) {
                    //重写密码为空，防止密码在缓存端泄露
                    customer.setCustomerPassword("null");
                    String takenForont = UUIDModel.randomUUID();
                    redisTemplate.opsForValue().set(takenForont, customer, 30, TimeUnit.MINUTES);
                    log.info(  String.valueOf( (CustomerEntity) redisTemplate.opsForValue().get(takenForont) ) );
                    return WebResponse.success("登录成功", "token{"+takenForont+"}");
                }
            }
        } catch (Exception e) {
            log.error( (e.getStackTrace()[0]).getMethodName()+" "+ ( e.getStackTrace()[0]).getLineNumber() +" " + e);
            return WebResponse.bizError("服务器异常，请联系管理人员");
        }
        return WebResponse.bizError("登录失败，用户名或密码错误");
    }
    //  2fd284c173bfdce8bf2f794fc532f34a860c4706킒⏺覅뚞脓퍰䛴娯6

    //注册
    @Override
    public WebResponse userRegisteredService(UserEntity userEntity) {
        try {
            if (userEntity.getUserPassword() == null || userEntity.getUserPassword().length() < 6 ||
                    userEntity.getUserReal() == null || userEntity.getUserName() == null ||
                    userEntity.getUserMobilephone() == null || userEntity.getUserMobilephone().length() < 7 ||
                    userEntity.getUserEmail() == null || userEntity.getUserEmail().length() < 6) {
                return WebResponse.errorOther("请按要求填写");
            }
            if (userDao.getExistEmailDao(userEntity) > 0) {
                return WebResponse.errorOther("该邮箱已被使用");
            }
            if (userDao.getExistMobilephoneDao(userEntity) > 0) {
                return WebResponse.errorOther("该手机号已被使用");
            }
            String uuid = UUIDModel.randomUUID();
            userEntity.setUserId(uuid);
            userEntity.setUserPassword(Md5AndSha1.randomMD5AndSHA1(userEntity.getUserPassword()));
            log.info(userEntity.getUserPassword().length() + "  " + userEntity.getUserPassword());
            if (userDao.addUserDao(userEntity) == 1) {
                return WebResponse.success("注册成功");
            }
        } catch (Exception e) {
            log.error(" userLoginService： " + String.valueOf(e));
            return WebResponse.bizError("服务器异常，请联系管理人员");
        }
        return WebResponse.success("注册失败");
    }

    @Override //修改密码
    public WebResponse userUpdatePassWordService(UserEntity userEntity) {
        CustomerEntity customerEntity = (CustomerEntity) redisTemplate.opsForValue().get(userEntity.getTokenFront());
        customerEntity.setCustomerPassword(userEntity.getUserPassword());
        customerEntity.setNewCustomerPassword(userEntity.getNewuserPassword());
        if (Md5AndSha1.verifyMD5AndSHA1(customerEntity.getCustomerPassword(), userDao.getUserOneNeedcustomerIdDao(customerEntity).getCustomerPassword())) {
            if (userDao.updateUserPassWordDao(customerEntity) == 1) {
                return WebResponse.success("修改成功");
            }
            return WebResponse.bizError("修改错误");
        }
        return WebResponse.bizError("原密码错误");
    }

    @Override  //修改资料
    public WebResponse userUpdateAllService(UserEntity userEntity) {
        try{
        CustomerEntity customerEntity = (CustomerEntity) redisTemplate.opsForValue().get(userEntity.getTokenFront());
        userEntity.setUserId(customerEntity.getCustomerId());
        if (userDao.updateUserAllDao(userEntity) == 2) {
            return WebResponse.success("修改成功");
        }
        return WebResponse.bizError("修改失败");
        }catch (Exception e){
            log.error(" userUpdateAllService: "+e);
            return WebResponse.bizError("服务器异常，请联系管理员");
        }
    }


}
